package com.jt.common.util;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;

public class SMSUtil {
	static Logger log = LoggerFactory.getLogger(SMSUtil.class);
	final static String url = "http://211.147.239.62:9050/cgi-bin/sendsms";
	final static String username = "Gofun@sqzx";
	final static  String password = "4rfv5tgb@YHN";
	
	final static String yx_url = "http://211.147.239.62:9050/cgi-bin/sendsms";
	final static String yx_username = "bjsqzx@bjsqzx";
	final static  String yx_password = "4rfv5tgb@YHN";
	
	
	
	public static void main(String[] args) throws IOException {
        String content = "尊敬的客户，您的反馈已经处理完成，给您带来的不便我们深表歉意！感谢您对我们提出的宝贵意见。";
		System.out.println("-----" + send("11", content));
//		sendBatchYx(Lists.newArrayList(new String[]{"18600070286"}), content);
	}
	
	public static String send(String mobile, String content)  {
		   String tmpContent = content;
		   try {
			   content = URLEncoder.encode(tmpContent, "GB2312"); 
	            StringBuilder rUrl = new StringBuilder();
	            rUrl.append(url).append("?username=").append(username)
	                    .append("&password=").append(password)
	                    .append("&to=").append(mobile)
	                    .append("&text=").append(content)
	                    .append("&msgtype=1");
	            return HttpClient.doGet(rUrl.toString());
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			return e.getMessage();
		}
	  }
	 
	/**
	 * 营销短信通道
	 * @param mobileList
	 * @param content
	 * @throws IOException
	 */
	 public static void sendBatchYx(List<String> mobileList, String content) {
		  String tmpContent = content;
		  try {
			  content = URLEncoder.encode(tmpContent, "GB2312"); 
		         for (String mobile : mobileList) {
		             StringBuilder rUrl = new StringBuilder();
		             rUrl.append(yx_url).append("?username=").append(yx_username)
		                     .append("&password=").append(yx_password)
		                     .append("&to=").append(mobile)
		                     .append("&text=").append(content)
		                     .append("&msgtype=1");
		             
		             String result = HttpClient.doGet(rUrl.toString());
		             
		             log.info("发送短信至:" + mobile + ",短信内容:" + tmpContent + ",result=" + result);
		         }
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	      
	 }
	 
	 /**
	  * 营销短信通道 扩展方法， 
	  * @param mobileList
	  * @param content
	  */
	 public static String sendShortMsgYx(String mobile, String content) {
		 try{
			 content = URLEncoder.encode(content, "GB2312");
			 StringBuilder rUrl = new StringBuilder();
             rUrl.append(yx_url).append("?username=").append(yx_username)
                     .append("&password=").append(yx_password)
                     .append("&to=").append(mobile)
                     .append("&text=").append(content)
                     .append("&msgtype=1");
             return HttpClient.doGet(rUrl.toString());
		 }catch (Exception e) {
			log.error(e.getMessage(),e);
			return e.getMessage();
		}
	 }
	 
	/**
	 * 营销短信通道
	 * @param phone
	 * @param content
	 * @throws IOException
	 */
	 public static void sendYx(String phone, String content) {
		  String tmpContent = content;
		  try {
			  content = URLEncoder.encode(tmpContent, "GB2312"); 
		         StringBuilder rUrl = new StringBuilder();
		         rUrl.append(yx_url).append("?username=").append(yx_username)
		         .append("&password=").append(yx_password)
		         .append("&to=").append(phone)
		         .append("&text=").append(content)
		         .append("&msgtype=1");
		         String result = HttpClient.doGet(rUrl.toString());
		         
		         log.info("发送短信至:" + phone + ",短信内容:" + tmpContent + ",result=" + result);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
		}
	      
	 }

	private static final int CPU_SIZE = Runtime.getRuntime().availableProcessors();
	private static final int GROUP_SIZE = 5000;//每组个数
	/**
	 * 大批量发送（默认）
	 *
	 * @param simList sim
	 * @param content 内容
	 * @throws InterruptedException
	 */
	public static void sendHuge(List<String> simList,String content) throws InterruptedException {
		sendHuge(simList,content,CPU_SIZE ,GROUP_SIZE);
	}

	/**
	 * 大批量发送（自定义）
	 * @param simList
	 * @param content
	 * @param Pool_size 线程池数
	 * @param group_size 分组 每组个数
	 * @throws InterruptedException
     */
	public static void sendHuge(List<String> simList, final String content, int Pool_size, int group_size) throws InterruptedException {
		//分组
		final List<List<String>> groupVo = Lists.partition(simList, group_size);
		//计数
		final CountDownLatch latch = new CountDownLatch(groupVo.size());
		log.info("批量发送短信开始！");
		ExecutorService executorPool = Executors.newFixedThreadPool(Pool_size);
		//多个线程执行推送
		for (int i = 0; i < groupVo.size(); i++) {
			final int index = i;
			executorPool.execute(new Runnable() {
				@Override
				public void run() {
					try {
						sendBatchYx(groupVo.get(index),content);
                        /*基于组推送*/
					} catch (Exception e) {
						log.info("批量发送第" + index + "组执行出现异常：" + e.getMessage());
					} finally {
						latch.countDown();
					}
				}
			});
		}
		latch.await();
		log.info("批量发送短信结束！共发送"+simList.size()+"用户");
	}

}
